<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Exif 信息查看</title>
    <link rel="stylesheet" href="../../bootstrap/bootstrap.min.css">
</head>

<body>
    <div class="container mt-5">
        <h1 class="mb-4">Exif 信息查看</h1>
        <form>
            <div class="form-group">
                <label for="imageInput">选择要查看的图片：</label>
                <input type="file" id="imageInput">
            </div>
            <button type="button" class="btn btn-primary" onclick="showExif()">查看</button>
        </form>
        <div class="mt-4">
            <h2>Exif 信息</h2>
            <table class="table table-bordered" id="exifTable" style="display: none;">
                <tbody>
                    <tr>
                        <th>标签</th>
                        <td id="exifLabel"></td>
                    </tr>
                    <tr>
                        <th>值</th>
                        <td id="exifValue"></td>
                    </tr>
                </tbody>
            </table>
            <p id="error" style="display: none; color: red;">无法解析该图片的 Exif 信息。</p>
        </div>
    </div>
    <script src="https://cdn.bootcss.com/exif-js/2.3.0/exif.js"></script>
    <script>
        function showExif() {
            const file = document.getElementById("imageInput").files[0];
            if (!file) {
                alert("请选择要查看的图片。");
                return;
            }
            const reader = new FileReader();
            reader.onload = function (e) {
                try {
                    const exifData = Exif.parseFromBinaryString(e.target.result);
                    const exifTable = document.getElementById("exifTable");
                    const exifLabel = document.getElementById("exifLabel");
                    const exifValue = document.getElementById("exifValue");
                    exifTable.style.display = "table";
                    exifLabel.innerHTML = "";
                    exifValue.innerHTML = "";
                    for (const key in exifData) {
                        if (exifData.hasOwnProperty(key)) {
                            exifLabel.innerHTML += `${key}<br>`;
                            exifValue.innerHTML += `${exifData[key]}<br>`;
                        }
                    }
                } catch (err) {
                    document.getElementById("exifTable").style.display = "none";
                    document.getElementById("error").style.display = "block";
                }
            };
            reader.readAsBinaryString(file);
        }
    </script>
</body>

</html>